package com.zhiyou.aifeng.smart_machine

import android.annotation.SuppressLint
import android.os.Bundle
import com.aifeng.utils.getExtra
import com.jeremyliao.liveeventbus.LiveEventBus
import com.zhiyou.aifeng.common.base.makeCall
import com.zhiyou.aifeng.common.base.oneClick
import com.zhiyou.aifeng.common.helper.*
import com.zhiyou.aifeng.common_ui.base.BaseActivity
import com.zhiyou.aifeng.common_ui.model.BaseUrl
import com.zhiyou.aifeng.common_ui.utils.showHintDialog
import com.zhiyou.aifeng.viewmodel.MatterViewModel
import kotlinx.android.synthetic.main.activity_matter_detail.*
import org.jetbrains.anko.toast
import org.json.JSONObject

class MatterDetailActivity : BaseActivity() {

    private var mMatterCode: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_matter_detail)
        initTitle(title = "订单详情", divider = false)

        getData()
    }

    override fun getViewModel() = get<MatterViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        matter_call.oneClick {
            if (getString("auth") != "2") {
                toast("您还未通过实名认证，暂无法操作！")
                return@oneClick
            }

            if (matter_tel.textString().isNotEmpty()) {
                showHintDialog(
                    title = "提示",
                    hint = "确定要拨打联系农户吗？"
                ) { makeCall(matter_tel.textString()) }
            }
        }

        bt_grab.oneClick {
            mMatterCode?.let {
                if (getString("auth") != "2") {
                    toast("您还未通过实名认证，暂无法操作！")
                    return@oneClick
                }

                getViewModel().request(
                    requestUrl = BaseUrl.workGet,
                    params = *arrayOf(
                        "uid" to getString("userId"),
                        "njsjid" to intent.getExtra("orderId")
                    ),
                    onResult = {
                        toast("确认接单成功！")
                        LiveEventBus.get("matterList").post(intent.getExtra("orderId"))
                        finishView()
                    }
                )
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().request(
            requestUrl = BaseUrl.workInfo,
            params = *arrayOf(
                "njsjid" to intent.getExtra("orderId")
            ),
            onResult = {
                val obj = JSONObject(it).optJSONObject("workTask") ?: JSONObject()

                mMatterCode = obj.optNotNull("work_code")
                matter_num.text = "订单编号：$mMatterCode"
                matter_type.text = obj.optNotNull("work_type_name")
                matter_square.text = "${obj.optNotNull("area").formatDecimal()}亩"
                matter_price.text = "${obj.optNotNull("price").formatDecimal()}元/亩"
                matter_time.text =
                    "${obj.optNotNull("preset_date").getTimeString("yyyy-MM-dd HH:mm")}（预计）"
                matter_adress.text =
                    obj.optNotNull("region_addr").replace(" ", "") + obj.optNotNull("address")

                matter_name.text = obj.optNotNull("contact")
                matter_tel.text = obj.optNotNull("contact_phone")
            }
        )
    }

}